<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>资讯列表</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="/static/layuiadmin/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="/static/layuiadmin/style/admin.css" media="all">

</head>
<body>

<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-form layui-card-header layuiadmin-card-header-auto">
            <div class="layui-form-item">
                <div class="layui-inline">所属分类</div>
                <div class="layui-inline">
                    <select name="art_category_id" lay-verify="">
                        <option value="-1" >全部</option>
                        {volist name="categoryList" id="vo"}
                        <option value="{$vo['acc_id']}" >{$vo['acc_name']}</option>
                        {/volist}
                    </select>
                </div>
                <div class="layui-inline">审核状态</div>
                <div class="layui-inline">
                    <select name="art_ischeck" lay-verify="">
                        <option value="-1" >全部</option>
                        <option value="1" >已审核</option>
                        <option value="2" >未审核</option>

                    </select>
                </div>

                <div class="layui-inline">所属机构</div>
                <div class="layui-inline">
                    <select name="art_institution_id" lay-verify="">

                        {if !$isInstitutionAdmin}
                        <option value="-1" >全部</option>
                        {/if}
                        {volist name="institutionData" id="vo"}
                        <option value="{$vo['ins_id']}" >{$vo['ins_name']}</option>
                        {/volist}
                    </select>
                </div>

                <div class="layui-inline">文章标题</div>
                <div class="layui-inline">
                    <input type="text" name="art_title" placeholder="请输入" autocomplete="off" class="layui-input">
                </div>
                <div class="layui-inline">
                    <button class="layui-btn layuiadmin-btn-admin" lay-submit lay-filter="LAY-task-back-search">
                        <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
                    </button>
                </div>
            </div>
        </div>

        <div class="layui-card-body">
            <div style="padding-bottom: 10px;">
                <button class="layui-btn layuiadmin-btn-admin" data-type="add">添加</button>
            </div>

            <table id="table" lay-filter="table"></table>

            <script type="text/html" id="ishot">
                {{#  if(d.art_ishot == 1){ }}
                <button class="layui-btn layui-btn-xs">已热门</button>
                {{#  } else { }}
                <button class="layui-btn layui-btn-primary layui-btn-xs">未热门</button>
                {{#  } }}
            </script>
            <script type="text/html" id="ischeck">
                {{#  if(d.art_ischeck == 1){ }}
                <button class="layui-btn layui-btn-xs">已审核</button>
                {{#  } else { }}
                <button class="layui-btn layui-btn-primary layui-btn-xs">未审核</button>
                {{#  } }}
            </script>
            <script type="text/html" id="buttonTpl">
                {{#  if(d.art_istop == 1){ }}
                <button class="layui-btn layui-btn-xs">是</button>
                {{#  } else { }}
                <button class="layui-btn layui-btn-primary layui-btn-xs">否</button>
                {{#  } }}
            </script>
            <script type="text/html" id="table-task">
                <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit"><i
                        class="layui-icon layui-icon-edit"></i>编辑</a>
                <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i
                        class="layui-icon  layui-icon-delete"></i>删除</a>
            </script>
        </div>
    </div>
</div>

<script src="/static/layuiadmin/layui/layui.js"></script>
<script>
    layui.config({
        base: '/static/layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'useradmin', 'table'], function () {
        var $ = layui.$
            , form = layui.form
            , table = layui.table;

        table.render({
            elem: '#table'
            , url: "{:url ('admin/article/articleList')}"
            , page: true
            , cols: [[
                {field: 'art_id', width: 80, title: 'ID', sort: true}
                , {field: 'art_title', title: '标题' ,align:"center"}
                , {field: 'ins_name', title: '机构名称' ,align:"center"}
                , {field: 'art_category', title: '所属分类' ,align:"center"}
                , {field: 'art_create_time', title: '创建时间' ,align:"center" }
                , {field: 'art_sort', title: '排序' ,align:"center",sort:true}
                , {field: 'art_view_times', title: '浏览次数',align:"center",sort:true}
                , {field: 'art_ishot', title: '是否热门',align:"center",toolbar:"#ishot"}
                , {field: 'art_ischeck', title: '是否审核',align:"center",toolbar:"#ischeck"}
                , {title: '操作', width: 150, align: 'center', fixed: 'right', toolbar: '#table-task'}
            ]]
            , text: {
                none: '暂无相关数据' //默认：无数据。注：该属性为 layui 2.2.5 开始新增
            }

        });

        //监听搜索
        form.on('submit(LAY-task-back-search)', function (data) {
            var field = data.field;
            //执行重载
            table.reload('table', {
                page: {
                    curr: 1 //重新从第 1 页开始
                }
                , where: {
                    "art_category_id": field.art_category_id,
                    "art_title":field.art_title,
                    "art_institution_id":field.art_institution_id,
                    "art_ischeck":field.art_ischeck
                }
            });
        });

        //监听工具条
        table.on('tool(table)', function (obj) {
            var data = obj.data;
            var art_id=data.art_id;
            if (obj.event === 'del') {
                layer.confirm('确定删除此篇文章？', function (index) {
                    //获取当前管理员的id
                    var data={
                        "art_id":art_id
                    };
                    $.ajax({
                        url: "{:url ('admin/article/articleDel')}",
                        type: "post",
                        dataType: "json",
                        data:data,
                        success: function (res) {
                            if (res.code == 1) {
                                layer.msg(res.msg, {icon: 1, time: 1500}, function () {
                                    obj.del();
                                });
                            } else {
                                layer.msg(res.msg, {icon: 5, time: 1500}, function () {
                                });
                            }
                        }
                    })
                    layer.close(index);
                });
            }
            else if (obj.event === 'edit') {
                layer.open({
                    type: 2
                    , title: '文章编辑'
                    , content: "{:url ('admin/article/articleEdit')}?art_id="+art_id
                    , area: ['1000px', '600px']
                    , btn: ['确定', '取消']
                    , yes: function (index, layero) {
                        var iframeWindow = window['layui-layer-iframe' + index]
                            , submitID = 'LAY-article-edit-submit'
                            , submit = layero.find('iframe').contents().find('#' + submitID);

                        //监听提交
                        iframeWindow.layui.form.on('submit(' + submitID + ')', function (data) {
                            var field = data.field; //获取提交的字段
                            $.ajax({
                                url: "{:url ('admin/article/articleEdit')}?XDEBUG_SESSION_START=10140&art_id="+art_id,
                                type: "post",
                                dataType: "json",
                                data:field,
                                success: function (res) {
                                    if (res.code == 1) {
                                        layer.close(index); //关闭弹层
                                        layer.msg(res.msg, {icon: 1, time: 1500}, function () {
                                            table.reload('table'); //数据刷新

                                        });
                                    } else {
                                        layer.msg(res.msg, {icon: 5, time: 1500}, function () {
                                        });
                                    }
                                }
                            })
                            return false;
                        });
                        submit.trigger('click');
                    }
                    , success: function (layero, index) {

                    }
                })
            }
        });

        //事件(table上面的添加功能)
        var active = {
            add: function () {
                layer.open({
                    type: 2
                    , title: '添加文章'
                    , content: "{:url ('admin/article/articleAdd')}"
                    , area: ['1000px', '600px']
                    , btn: ['确定', '取消']
                    , yes: function (index, layero) {
                        var iframeWindow = window['layui-layer-iframe' + index]
                            , submitID = 'LAY-task-add-submit'
                            , submit = layero.find('iframe').contents().find('#' + submitID);

                        //监听提交
                        iframeWindow.layui.form.on('submit(' + submitID + ')', function (data) {
                            var field = data.field; //获取提交的字段
                            console.log(field);
                            $.ajax({
                                url: "{:url ('admin/article/articleAdd')}",
                                type: "post",
                                data: field,
                                dataType: "json",
                                success: function (res) {
                                    if (res.code == 1) {
                                        table.reload('table');
                                        layer.close(index); //关闭弹层
                                        layer.msg(res.msg, {icon: 1, time: 1500}, function () {
                                        });
                                    } else {
                                        layer.msg(res.msg, {icon: 5, time: 1500}, function () {
                                        });
                                    }
                                }
                            })
                            return false;
                        });

                        submit.trigger('click');
                    }
                });
            }
        }
        $('.layui-btn.layuiadmin-btn-admin').on('click', function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });
    });
</script>
</body>
</html>

